package com.xueyi.system.retrospect;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONArray;
import com.mysql.cj.jdbc.Driver;
import com.xueyi.common.core.context.SecurityContextHolder;
import com.xueyi.common.core.utils.StringUtils;
import com.xueyi.common.core.web.result.AjaxResult;
import com.xueyi.common.datasource.config.properties.SourceProperties;
import com.xueyi.common.security.utils.DictUtils;
import com.xueyi.common.security.utils.SourceUtils;
import com.xueyi.system.Product.domain.dto.ProductDto;
import com.xueyi.system.Product.service.IProductService;
import com.xueyi.system.ProductBatch.domain.dto.ProductBatchDto;
import com.xueyi.system.ProductBatch.domain.query.ProductBatchQuery;
import com.xueyi.system.ProductBatch.service.IProductBatchService;
import com.xueyi.system.api.dict.domain.dto.SysDictDataDto;
import com.xueyi.system.api.organize.domain.dto.SysDeptDto;
import com.xueyi.system.api.organize.domain.dto.SysEnterpriseDto;
import com.xueyi.system.api.organize.domain.query.SysDeptQuery;
import com.xueyi.system.api.source.domain.Source;
import com.xueyi.system.area.domain.dto.BaseAreaDto;
import com.xueyi.system.area.service.IBaseAreaService;
import com.xueyi.system.authority.service.ISysLoginService;
import com.xueyi.system.batchManager.domain.dto.BatchManagerDto;
import com.xueyi.system.batchManager.domain.query.BatchManagerQuery;
import com.xueyi.system.batchManager.service.IBatchManagerService;
import com.xueyi.system.company.domain.dto.CompanyDto;
import com.xueyi.system.company.domain.query.CompanyQuery;
import com.xueyi.system.company.service.ICompanyService;
import com.xueyi.system.companyManager.domain.dto.BaseCompanyManagerDto;
import com.xueyi.system.companyManager.domain.query.BaseCompanyManagerQuery;
import com.xueyi.system.companyManager.service.IBaseCompanyManagerService;
import com.xueyi.system.dict.service.ISysDictDataService;
import com.xueyi.system.harvestManager.domain.dto.HarvestManagerDto;
import com.xueyi.system.harvestManager.domain.po.HarvestManagerPo;
import com.xueyi.system.harvestManager.domain.query.HarvestManagerQuery;
import com.xueyi.system.harvestManager.service.IHarvestManagerService;
import com.xueyi.system.inspection.domain.po.InspectionPo;
import com.xueyi.system.inspection.service.IInspectionService;
import com.xueyi.system.material.domain.dto.MaterialDto;
import com.xueyi.system.material.domain.query.MaterialQuery;
import com.xueyi.system.material.service.IMaterialService;
import com.xueyi.system.organize.service.ISysDeptService;
import com.xueyi.system.originManager.domain.dto.OriginManagerDto;
import com.xueyi.system.originManager.domain.po.OriginManagerPo;
import com.xueyi.system.originManager.domain.query.OriginManagerQuery;
import com.xueyi.system.originManager.service.IOriginManagerService;
import com.xueyi.system.packingManage.domain.dto.PackingManageDto;
import com.xueyi.system.packingManage.domain.po.PackingManagePo;
import com.xueyi.system.packingManage.domain.query.PackingManageQuery;
import com.xueyi.system.packingManage.service.IPackingManageService;
import com.xueyi.system.plantManager.domain.dto.BasePlantManagerDto;
import com.xueyi.system.plantManager.service.IBasePlantManagerService;
import com.xueyi.system.productDetection.domain.po.ProductDetectionPo;
import com.xueyi.system.productDetection.service.IProductDetectionService;
import com.xueyi.system.productionManager.domain.dto.BaseProductionManagerDto;
import com.xueyi.system.productionManager.domain.query.BaseProductionManagerQuery;
import com.xueyi.system.productionManager.service.IBaseProductionManagerService;
import com.xueyi.system.release.domain.dto.ReleaseDto;
import com.xueyi.system.release.domain.query.ReleaseQuery;
import com.xueyi.system.release.service.IReleaseService;
import com.xueyi.system.seed.domain.dto.BaseSeedDto;
import com.xueyi.system.seed.service.IBaseSeedService;
import com.xueyi.system.seedDetection.domain.dto.SeedDetectionDto;
import com.xueyi.system.seedDetection.domain.query.SeedDetectionQuery;
import com.xueyi.system.seedDetection.service.ISeedDetectionService;
import com.xueyi.system.storeManager.domain.dto.StoreManagerDto;
import com.xueyi.system.storeManager.domain.query.StoreManagerQuery;
import com.xueyi.system.storeManager.service.IStoreManagerService;
import com.xueyi.system.traceabilityCode.domain.po.TraceabilityCodePo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;

/**
 * @author zlf
 * @version 1.0
 * @data 2023/9/20 14:20
 */

@RestController
@RequestMapping("/retrospect")
public class retrospectController {


    @Autowired
    private IBatchManagerService batchManagerService;

    @Autowired
    private IPackingManageService packingManageService;

    @Autowired
    private ISysLoginService loginService;

    @Autowired
    private IProductBatchService productBatchService;

    @Autowired
    private IProductDetectionService productDetectionService;

    @Autowired
    private IHarvestManagerService harvestManagerService;

    @Autowired
    private ICompanyService companyService;

    @Autowired
    private IOriginManagerService originManagerService;

    @Autowired
    private IProductService productService;

    @Autowired
    private IBaseCompanyManagerService baseCompanyManagerService;

    @Autowired
    SourceProperties sourceProperties;

    @Autowired
    private IBasePlantManagerService plantManagerService;

    @Autowired
    private ISysDeptService deptService;

    @Autowired
    private IBaseProductionManagerService productionManagerService;

    @Autowired
    private ISeedDetectionService ISeedDetectionService;

    @Autowired
    private IBaseAreaService areaService;

    @Autowired
    private IStoreManagerService storeManagerService;

    @Autowired
    private IBaseSeedService baseSeedService;

    @Autowired
    private IInspectionService iInspectionService;

    @Autowired
    private ISysDictDataService dictDataService;

    @Autowired
    private IReleaseService releaseService;

    @Autowired
    private IMaterialService materialService;


    private static String url = "jdbc:mysql://112.132.1.213:3306/gegen-cloud?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8";
    private static String username = "gegen-cloud";
    private static String password = "2wxFptrKrYyCYhbX";



    @GetMapping("/traceBatch")
    public AjaxResult traceBatch(String code) {
        try {
            Connection connect = connect();
            // 创建查询语句
            String query = "SELECT * FROM base_code where code= ?";
            // 创建PreparedStatement对象
            PreparedStatement pstmt = connect.prepareStatement(query);
            pstmt.setString(1, code);
            // 执行查询
            ResultSet rs = pstmt.executeQuery();
            if (!rs.isBeforeFirst()) {
                return AjaxResult.error("查询失败！溯源码不需存在");
            }
            String enterpriseId = null;
            String plantId = null;
            String productId = null;
            String recovery = null;
            TraceabilityCodePo traceabilityCodePo = new TraceabilityCodePo();
            // 遍历查询结果
            while (rs.next()) {
                enterpriseId = rs.getString("tenant_id");
                plantId = rs.getString("plant_id");
                productId = rs.getString("product_id");
                String pack_id = rs.getString("pack_id");
                String batch_id = rs.getString("batch_id");
                String batch_name = rs.getString("batch_name");
                String pack_name = rs.getString("pack_name");
                String product_id = rs.getString("product_id");
                String product_name = rs.getString("product_name");
                recovery = rs.getString("recovery");
                String expiration = rs.getString("expiration");
                traceabilityCodePo.setPackId(pack_id);
                traceabilityCodePo.setPackName(pack_name);
                traceabilityCodePo.setBatchId(batch_id);
                traceabilityCodePo.setBatchName(batch_name);
                traceabilityCodePo.setProductId(product_id);
                traceabilityCodePo.setProductName(product_name);
                traceabilityCodePo.setExpiration(expiration);
            }


            SysEnterpriseDto enterprise = loginService.loginByEnterpriseId(enterpriseId);
            SecurityContextHolder.setIsLessor(enterprise.getIsLessor());
            Source source = SourceUtils.getSourceCache(enterprise.getStrategyId());
            SecurityContextHolder.setSourceName(source.getMaster());
            SecurityContextHolder.setEnterpriseId(enterprise.getId().toString());
            Map map = new HashMap<>();
            ProductDto productDto = productService.selectById(productId);
            CompanyQuery companyQuery = new CompanyQuery();
            List<CompanyDto> companyDtoList = companyService.selectList(companyQuery);
            BaseProductionManagerQuery baseProductionManagerQuery = new BaseProductionManagerQuery();
            baseProductionManagerQuery.setPlantId(plantId);
            List<BaseProductionManagerDto> baseProductionManagerDtos = productionManagerService.selectList(baseProductionManagerQuery);

            ProductBatchQuery productBatchQuery = new ProductBatchQuery();
            productBatchQuery.setPlantId(plantId);
            List<ProductBatchDto> productBatchDtos = productBatchService.selectList(productBatchQuery);
            List<ProductDetectionPo> productDetectionPo = productDetectionService.getInfo(plantId);

            BaseCompanyManagerQuery baseCompanyManagerQuery = new BaseCompanyManagerQuery();
            baseCompanyManagerQuery.setCompanyId(companyDtoList.get(0).getId().toString());
            List<BaseCompanyManagerDto> baseCompanyManagerDtoList = baseCompanyManagerService.selectList(baseCompanyManagerQuery);
            PackingManageQuery packingManageQuery = new PackingManageQuery();
            packingManageQuery.setPlantId(plantId);
            List<PackingManageDto> packingManageDtos = packingManageService.selectList(packingManageQuery);

            HarvestManagerQuery harvestManagerQuery = new HarvestManagerQuery();
            harvestManagerQuery.setPlantId(plantId);
            List<HarvestManagerDto> harvestManagerDtoList = harvestManagerService.selectList(harvestManagerQuery);

            OriginManagerQuery originManagerQuery = new OriginManagerQuery();
            originManagerQuery.setPlantId(plantId);
            List<OriginManagerDto> originManagerDtos = originManagerService.selectList(originManagerQuery);

            BasePlantManagerDto basePlantManagerDto = plantManagerService.selectById(plantId);

            List arrayList = new ArrayList<>();
            List<BaseProductionManagerDto> productionManagerDtoList = new ArrayList<>();
            BaseSeedDto baseSeedDto = null;
            if (basePlantManagerDto != null) {
                BaseProductionManagerQuery managerQuery = new BaseProductionManagerQuery();
                managerQuery.setPlantId(basePlantManagerDto.getId() + "");
                productionManagerDtoList = productionManagerService.selectList(managerQuery);
                List<String> list = JSONArray.parseArray(basePlantManagerDto.getAreaId().toString(), String.class);
                for (String s : list) {
                    BaseAreaDto baseAreaDto = areaService.selectById(s);
                    arrayList.add(baseAreaDto);
                }
                baseSeedDto = baseSeedService.selectById(basePlantManagerDto.getSeedId());
            }

            BatchManagerQuery batchManagerQuery = new BatchManagerQuery();
            batchManagerQuery.setPlantId(plantId);
            List<BatchManagerDto> batchManagerDtos = batchManagerService.selectList(batchManagerQuery);
            SysDeptQuery sysDeptQuery = new SysDeptQuery();
            sysDeptQuery.setCode("JFZY-SCJS");
            List<SysDeptDto> sysDeptDtos = deptService.selectList(sysDeptQuery);
            sysDeptQuery.setCode("JFZY-ZLGL");
            List<SysDeptDto> sysDeptDtos1 = deptService.selectList(sysDeptQuery);

            StoreManagerQuery storeManagerQuery = new StoreManagerQuery();
            storeManagerQuery.setPlantId(plantId);
            List<StoreManagerDto> storeManagerDtoList = storeManagerService.selectList(storeManagerQuery);

            List<InspectionPo> inspectionPoList = iInspectionService.findByList(plantId);

            ProductBatchDto productBatchDto = productBatchService.selectById(traceabilityCodePo.getBatchId());
            SeedDetectionQuery seedDetectionQuery = new SeedDetectionQuery();
            seedDetectionQuery.setPlantId(plantId);
            List<SeedDetectionDto> seedDetectionDtos = ISeedDetectionService.selectList(seedDetectionQuery);
            ReleaseQuery releaseQuery = new ReleaseQuery();
            releaseQuery.setPlantId(traceabilityCodePo.getPlantId());
            List<ReleaseDto> releaseDtos = releaseService.selectList(releaseQuery);


            map.put("basePlantManagerDto", basePlantManagerDto);
            map.put("seedDetectionDtos", seedDetectionDtos);
            map.put("releaseDtos", releaseDtos);
            map.put("productBatchDto", productBatchDto);
            map.put("inspectionPoList", inspectionPoList);
            map.put("storeManagerDtoList", storeManagerDtoList);
            map.put("recovery", recovery);
            map.put("traceabilityCodePo", traceabilityCodePo);
            map.put("product", sysDeptDtos.size() == 0 ? null : sysDeptDtos.get(0));
            map.put("quality", sysDeptDtos1.size() == 0 ? null : sysDeptDtos1.get(0));
            map.put("productDto", productDto);
            map.put("companyDtoList", companyDtoList);
            map.put("baseProductionManagerDtos", baseProductionManagerDtos);
            map.put("productBatchDtos", productBatchDtos);
            map.put("packingManageDtos", packingManageDtos);
            map.put("batchManagerDtos", batchManagerDtos);
            map.put("originManagerDtos", originManagerDtos);
            map.put("productDetectionPo", productDetectionPo);
            map.put("baseCompanyManagerDtoList", baseCompanyManagerDtoList);
            map.put("harvestManagerDtoList", harvestManagerDtoList);
            map.put("enterpriseId", enterpriseId);
            map.put("baseSeedDto", baseSeedDto);
            map.put("releaseDtos", releaseDtos);
            map.put("productionManagerDtoList", productionManagerDtoList);
            map.put("BaseAreaDtos", arrayList);
            return AjaxResult.success(map);
        } catch (Exception e) {
            System.out.println("系统异常=== "+e);
            return AjaxResult.error("系统异常");
        }
    }

    /**
     * 溯源
     *
     * @return
     */
    @GetMapping("/traceabilityCode")
    public AjaxResult traceabilityCode(String code) {
        try {

            if(StringUtils.isEmpty(code)){
                return AjaxResult.error("查询失败！溯源码为空");
            }

            Connection connect = connect();
            // 创建查询语句
            String query = "SELECT * FROM base_code where code= ?";
            // 创建PreparedStatement对象
            PreparedStatement pstmt = connect.prepareStatement(query);
            pstmt.setString(1, code);
            // 执行查询
            ResultSet rs = pstmt.executeQuery();
            if (!rs.isBeforeFirst()) {
                return AjaxResult.error("查询失败！溯源码不存在");
            }
            String enterpriseId = null;
            String plantId = null;
            String productId = null;
            String recovery = null;
            TraceabilityCodePo traceabilityCodePo = new TraceabilityCodePo();
            // 遍历查询结果
            while (rs.next()) {
                enterpriseId = rs.getString("tenant_id");
                plantId = rs.getString("plant_id");
                if(StringUtils.isEmpty(rs.getString("plant_id"))){
                   return AjaxResult.error(" 查询失败！溯源码错误或未绑定");
                }
                productId = rs.getString("product_id");
                String pack_id = rs.getString("pack_id");
                String batch_id = rs.getString("batch_id");
                String batch_name = rs.getString("batch_name");
                String pack_name = rs.getString("pack_name");
                String product_id = rs.getString("product_id");
                String product_name = rs.getString("product_name");
                recovery = rs.getString("recovery");
                String expiration = rs.getString("expiration");
                traceabilityCodePo.setPlantId(plantId);
                traceabilityCodePo.setPackId(pack_id);
                traceabilityCodePo.setPackName(pack_name);
                traceabilityCodePo.setBatchId(batch_id);
                traceabilityCodePo.setBatchName(batch_name);
                traceabilityCodePo.setProductId(product_id);
                traceabilityCodePo.setProductName(product_name);
                traceabilityCodePo.setExpiration(expiration);
            }

            SysEnterpriseDto enterprise = loginService.loginByEnterpriseId(enterpriseId);
            SecurityContextHolder.setIsLessor(enterprise.getIsLessor());
            Source source = SourceUtils.getSourceCache(enterprise.getStrategyId());
            SecurityContextHolder.setSourceName(source.getMaster());
            SecurityContextHolder.setEnterpriseId(enterprise.getId().toString());
            Map map = new HashMap<>();
            List<CompanyDto> companyDtoList = companyService.selectList(new CompanyQuery());
            List<BaseCompanyManagerDto> baseCompanyManagerDtos = baseCompanyManagerService.selectList(new BaseCompanyManagerQuery());

            BatchManagerDto batchManagerDto = null;
            HarvestManagerPo harvestManagerPo = null;
            OriginManagerPo originManagerPos = null;
            BaseSeedDto baseSeedDto = null;
            List arrayList = new ArrayList<>();
            List<ReleaseDto> releaseDtos = new ArrayList<>();
            List<BaseProductionManagerDto> productionManagerDtoList = new ArrayList<>();
            PackingManagePo packingManageInfo = packingManageService.selectById(traceabilityCodePo.getPackId());
            if (packingManageInfo != null) {
                batchManagerDto = batchManagerService.selectById(packingManageInfo.getBatchId());
                ReleaseQuery releaseQuery = new ReleaseQuery();
                releaseQuery.setPackId(packingManageInfo.getId());
                releaseDtos = releaseService.selectList(releaseQuery);
                if (batchManagerDto != null) {
                    originManagerPos = originManagerService.selectById(batchManagerDto.getProcessBatchCode());
                    if (originManagerPos != null) {
                        harvestManagerPo = harvestManagerService.selectById(originManagerPos.getHarvestId());
                        if (harvestManagerPo != null) {
                            BasePlantManagerDto basePlantManagerDto = plantManagerService.selectById(plantId);
                            if (basePlantManagerDto != null) {
                                BaseProductionManagerQuery managerQuery = new BaseProductionManagerQuery();
                                managerQuery.setPlantId(basePlantManagerDto.getId() + "");
                                productionManagerDtoList = productionManagerService.selectList(managerQuery);
                                List<String> list = JSONArray.parseArray(basePlantManagerDto.getAreaId().toString(), String.class);
                                for (String s : list) {
                                    BaseAreaDto baseAreaDto = areaService.selectById(s);
                                    arrayList.add(baseAreaDto);
                                }
                                baseSeedDto = baseSeedService.selectById(basePlantManagerDto.getSeedId());
                            }
                        }
                    }
                }
            }
            ProductBatchDto productBatchDto = productBatchService.selectById(traceabilityCodePo.getBatchId());
            ProductDto productDto = productService.selectById(traceabilityCodePo.getProductId());
            List<ProductDetectionPo> productDetectionPo = productDetectionService.getInfo(traceabilityCodePo.getPlantId());
            //List<ProductDto> productDtos = productService.selectList(new ProductQuery());
            List<BaseCompanyManagerDto> baseCompanyManagerDtoList = baseCompanyManagerService.selectList(new BaseCompanyManagerQuery());

            SeedDetectionQuery seedDetectionQuery = new SeedDetectionQuery();
            seedDetectionQuery.setPlantId(plantId);
            List<SeedDetectionDto> seedDetectionDtos = ISeedDetectionService.selectList(seedDetectionQuery);

            SysDeptQuery sysDeptQuery = new SysDeptQuery();
            sysDeptQuery.setCode("PD000102");
            List<SysDeptDto> sysDeptDtos = deptService.selectList(sysDeptQuery);
            sysDeptQuery.setCode("QA0000100");
            List<SysDeptDto> sysDeptDtos1 = deptService.selectList(sysDeptQuery);
            StoreManagerQuery storeManagerQuery = new StoreManagerQuery();
            storeManagerQuery.setPlantId(plantId);
            List<StoreManagerDto> storeManagerDtoList = storeManagerService.selectList(storeManagerQuery);

            List<InspectionPo> inspectionPoList = iInspectionService.findByList(plantId);

            BasePlantManagerDto basePlantManagerDto = plantManagerService.selectById(plantId);

            map.put("releaseDtos", releaseDtos);
            map.put("harvestManagerPo", harvestManagerPo);
            map.put("productionManagerDtoList", productionManagerDtoList);
            map.put("baseAreaDto", arrayList);
            map.put("baseSeedDto", baseSeedDto);
            map.put("originManagerPos", originManagerPos);
            map.put("batchManagerDto", batchManagerDto);
            map.put("basePlantManagerDto", basePlantManagerDto);
            map.put("product", sysDeptDtos.size() == 0 ? null:sysDeptDtos.get(0));
            map.put("quality",  sysDeptDtos1.size() == 0 ? null:sysDeptDtos1.get(0));
            map.put("seedDetectionDtos", seedDetectionDtos);
            map.put("inspectionPoList", inspectionPoList);
            map.put("baseCompanyManagerDtos", baseCompanyManagerDtos);
            map.put("recovery", recovery);
            map.put("storeManagerDtoList", storeManagerDtoList);
            map.put("companyDtoList", companyDtoList);
            map.put("traceabilityCodePo", traceabilityCodePo);
            map.put("packingManageInfo", packingManageInfo);
            map.put("productDto", productDto);
            map.put("productDetectionPo", productDetectionPo);
            map.put("productBatchDto", productBatchDto);
            map.put("baseCompanyManagerDtoList", baseCompanyManagerDtoList);
            map.put("enterpriseId", enterpriseId);

            return AjaxResult.success(map);
        } catch (
                Exception e) {
            return AjaxResult.error("系统异常");
        }
    }

    @GetMapping("/traceBatchPlantId")
    public AjaxResult traceBatchPlantId(String plantId, String enterpriseId) {
        try {
            SysEnterpriseDto enterprise = loginService.loginByEnterpriseId(enterpriseId);
            SecurityContextHolder.setIsLessor(enterprise.getIsLessor());
            Source source = SourceUtils.getSourceCache(enterprise.getStrategyId());
            SecurityContextHolder.setSourceName(source.getMaster());
            SecurityContextHolder.setEnterpriseId(enterprise.getId().toString());
            Map map = new HashMap<>();
            CompanyQuery companyQuery = new CompanyQuery();
            List<CompanyDto> companyDtoList = companyService.selectList(companyQuery);
            BaseProductionManagerQuery baseProductionManagerQuery = new BaseProductionManagerQuery();
            baseProductionManagerQuery.setPlantId(plantId);
            List<BaseProductionManagerDto> baseProductionManagerDtos = productionManagerService.selectList(baseProductionManagerQuery);

            ProductBatchQuery productBatchQuery = new ProductBatchQuery();
            productBatchQuery.setPlantId(plantId);
            List<ProductBatchDto> productBatchDtos = productBatchService.selectList(productBatchQuery);
            List<ProductDetectionPo> productDetectionPo = productDetectionService.getInfo(plantId);

            BaseCompanyManagerQuery baseCompanyManagerQuery = new BaseCompanyManagerQuery();
            baseCompanyManagerQuery.setCompanyId(companyDtoList.get(0).getId().toString());
            List<BaseCompanyManagerDto> baseCompanyManagerDtoList = baseCompanyManagerService.selectList(baseCompanyManagerQuery);
            PackingManageQuery packingManageQuery = new PackingManageQuery();
            packingManageQuery.setPlantId(plantId);
            List<PackingManageDto> packingManageDtos = packingManageService.selectList(packingManageQuery);

            HarvestManagerQuery harvestManagerQuery = new HarvestManagerQuery();
            harvestManagerQuery.setPlantId(plantId);
            List<HarvestManagerDto> harvestManagerDtoList = harvestManagerService.selectList(harvestManagerQuery);

            OriginManagerQuery originManagerQuery = new OriginManagerQuery();
            originManagerQuery.setPlantId(plantId);
            List<OriginManagerDto> originManagerDtos = originManagerService.selectList(originManagerQuery);

            // ProductDto productDto = productService.selectById(productBatchQuery.getProductId());

            BatchManagerQuery batchManagerQuery = new BatchManagerQuery();
            batchManagerQuery.setPlantId(plantId);
            List<BatchManagerDto> batchManagerDtos = batchManagerService.selectList(batchManagerQuery);
            SysDeptQuery sysDeptQuery = new SysDeptQuery();
            sysDeptQuery.setCode("PD000102");
            List<SysDeptDto> sysDeptDtos = deptService.selectList(sysDeptQuery);
            sysDeptQuery.setCode("QA0000100");
            List<SysDeptDto> sysDeptDtos1 = deptService.selectList(sysDeptQuery);

            map.put("product", sysDeptDtos.get(0));
            map.put("quality", sysDeptDtos1.get(0));
            //map.put("productDto", productDto);
            map.put("companyDtoList", companyDtoList);
            map.put("baseProductionManagerDtos", baseProductionManagerDtos);
            map.put("productBatchDtos", productBatchDtos);
            map.put("packingManageDtos", packingManageDtos);
            map.put("batchManagerDtos", batchManagerDtos);
            map.put("originManagerDtos", originManagerDtos);
            map.put("productDetectionPo", productDetectionPo);
            map.put("baseCompanyManagerDtoList", baseCompanyManagerDtoList);
            map.put("harvestManagerDtoList", harvestManagerDtoList);
            map.put("enterpriseId", enterpriseId);
            return AjaxResult.success(map);
        } catch (
                Exception e) {
            return AjaxResult.error("系统异常");
        }
    }


    /**
     * 根据字典类型查询字典数据信息
     */
    @GetMapping(value = "/types/{codeList}")
    public AjaxResult listByCodeList(@PathVariable List<String> codeList) {
        if (CollUtil.isEmpty(codeList))
            AjaxResult.error("请传入编码后再查询字典");
        HashMap<String, List<SysDictDataDto>> map = new HashMap<>();
        for (String code : codeList)
            map.put(code, DictUtils.getDictCache(code));
        return AjaxResult.success(map);
    }


    /**
     * 投入品库
     * @return
     */

    @GetMapping("/materialDtos")
    public AjaxResult materialDtos(){
        SysEnterpriseDto enterprise = loginService.loginByEnterpriseId("2");
        SecurityContextHolder.setIsLessor(enterprise.getIsLessor());
        Source source = SourceUtils.getSourceCache(enterprise.getStrategyId());
        SecurityContextHolder.setSourceName(source.getMaster());
        SecurityContextHolder.setEnterpriseId(enterprise.getId().toString());
        MaterialQuery materialQuery = new MaterialQuery();
        materialQuery.setDelFlag(0L);
        List<MaterialDto> materialDtos = materialService.selectList(materialQuery);
        return AjaxResult.success(materialDtos);
    }


    /**
     * 田间管理
     * @return
     * @throws SQLException
     */
    @GetMapping("/getProductionManager")
    public AjaxResult getProductionManagerService() {

        SysEnterpriseDto enterprise = loginService.loginByEnterpriseId("2");
        SecurityContextHolder.setIsLessor(enterprise.getIsLessor());
        Source source = SourceUtils.getSourceCache(enterprise.getStrategyId());
        SecurityContextHolder.setSourceName(source.getMaster());
        SecurityContextHolder.setEnterpriseId(enterprise.getId().toString());
        BaseProductionManagerQuery managerQuery = new BaseProductionManagerQuery();
        managerQuery.setDelFlag(0L);
        List<BaseProductionManagerDto> baseProductionManagerDtos = productionManagerService.selectList(managerQuery);
        Integer shifei = 0;
        Integer dayao = 0;
        Integer chucao = 0;
        Integer guanshui = 0;
        Integer zaizhong = 0;
        for (BaseProductionManagerDto baseProductionManagerDto : baseProductionManagerDtos) {
            if("5".equals(baseProductionManagerDto.getAgroType())){
                shifei++;
            }

            if("4".equals(baseProductionManagerDto.getAgroType())){
                dayao++;
            }

            if("6".equals(baseProductionManagerDto.getAgroType())){
                chucao++;
            }

            if("11".equals(baseProductionManagerDto.getAgroType())){
                guanshui++;
            }

            if("18".equals(baseProductionManagerDto.getAgroType())){
                zaizhong++;
            }
        }

        Map map = new HashMap<>();
        map.put("total",baseProductionManagerDtos.size());
        map.put("shifei",shifei);
        map.put("dayao",dayao);
        map.put("chucao",chucao);
        map.put("guanshui",guanshui);
        map.put("zaizhong",zaizhong);
        return AjaxResult.success(map);
    }






    //方式一，直接导入第三方库驱动类
    private Connection connect() throws SQLException {

        String username1 = sourceProperties.getUsername();
        System.out.println(username1);
        //获取驱动
        Driver driver = new Driver();
        //将用户名和密码放入到Properities对象中
        Properties properties = new Properties();
        properties.setProperty("user", username);//用户
        properties.setProperty("password", password);//密码
        final Connection connect = driver.connect(url, properties);
        System.out.println("1" + connect);
        return connect;
    }


}
